OCLOperators transform

object.transform(viewmodelname, <destination object>) uses the ViewModel to transform information from <object> into the <destination object>. Returns True if the transform is full. The transform operator takes the object as the root of the ViewModel specified and uses the ViewModel as a template to copy and create a new structure of objects.

Usually, a false return value, i.e. not a full transform, is due to the fact that the destination attribute could not be found or is read-only. Currently, there is no log available to pinpoint exactly why false is returned. Read about duck-typing below for hints on what is incorrect in your transformation.

Example of How to Use It

Often, when sending an email, you need to "collect" information from many objects to find all the information needed to create an outgoing email.

Using this model of an email queue, let's see how objects of the OutgoingEmail and EmailAttachment can be created using transform.

EmailQueueClassDiagram.png

This ViewModel and transform will fill in the OutgoingEmail and EmailAttachment by name-matching each attribute and link from the "Självfaktura" onto OutgoingEmail.

TransformViewmodelForEmail.png

(Please note the "trick" self->asSet because we need a list, not a link to fill in the "EmailAttachments" multi-link and Självfaktura only contains an attribute "Pdf".)

This is the EAL code in the "Send" action on "Självfaktura". Create the OutgoingEmail object and call transform to fill it in. If the transform isn't full (some attributes weren't stored), we back out by removing the newly created oe object.

let oe = OutgoingEmail.create in
(
  if self.transform('TR_SjälvfakturaSkickaPDF', oe) then
    true
  else
    oe.Delete
  endif
)

For completeness, this is the serverside ViewModel that sends emails and thus empties the queue.

ViewmodelForSendingEmail.png

Duck Typing

Transforming objects over the class border based on similarities - as transform does - is often referred to as Duck Typing. In MDriven, the ViewModel is used as a vehicle to facilitate a type-safe way to the duck type transform. The ViewModel Editor has a property to enable you to type-check your transform in design time during normal model validation:

2019-11-03 19h15 37.png
This page was edited 153 days ago on 06/17/2024. What links here